﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CharacterTrailClient.Windows.Exceptions;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using DevExpress.XtraEditors;

namespace CharacterTrailClient.Windows
{
    public partial class FormBomMainEditUpdate : DevExpress.XtraEditors.XtraForm
    {
        //private string _oldProductItemNumber = string.Empty;
        private readonly string _oldProductVersion = string.Empty;
        private readonly int _oldProductItemId = -1;
        private readonly string _oldMaterialItemNumber = string.Empty;
        private readonly int _oldMaterialItemId = -1;
        private readonly TBomEntryModel _bomEntryModel = null;
        private readonly IBom _bomImpl = InstanceFactory.GetBom();
        public FormBomMainEditUpdate(TBomEntryModel bomEntryModel)
        {
            if (bomEntryModel != null)
            {
                _bomEntryModel = bomEntryModel;

                _oldProductVersion = bomEntryModel.ProductVersion;
                _oldMaterialItemNumber = bomEntryModel.MaterialItemNumber;
                _oldProductItemId = bomEntryModel.ProductItemId;
                _oldMaterialItemId = bomEntryModel.MaterialItemId;
            }
            InitializeComponent();
            DataBinding();
        }

        private void DataBinding()
        {
            if (GlobalClass.RunTimeMode)
            {
                Binding.DataBinder.BindingProductItem(this.gridLookUpEditMaterialItemNumber, this.textEditMaterialItemId, this.textEditMaterialItemName);
                Binding.DataBinder.BindingProcedure(this.lookUpEditProcedureNo, this.textEditProcedureName);

                foreach (var control in groupControl1.Controls)
                {
                    if (control is BaseEdit)
                    {
                        var edit = (BaseEdit)control;
                        edit.DataBindings.Clear();
                        edit.DataBindings.Add("EditValue", _bomEntryModel, edit.Tag.ToString());
                    }
                }
            }
        }

        private void CmdSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Save())
                {
                    return;
                }
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception exception)
            {
              ExceptionHandler.ShowException(exception);
            }
        }

        private void CmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private bool ValidateBomEntryModel( out string msg)
        {
            msg = "";

            if(String.IsNullOrEmpty(this.lookUpEditProcedureNo.Text.Trim()))
            {
                msg = "工序没有选择！";
                return false;
            }

            decimal qty = 0;
            if (String.IsNullOrEmpty(this.calcEditQty.Text.Trim())
                || !decimal.TryParse(this.calcEditQty.Text, out qty)
                || qty < 1)
            {
                msg = "请输入正确的物料数量，必须不小于1！";
                return false;
            }

            if (_bomEntryModel.MaterialItemNumber == _bomEntryModel.ProductItemNumber)
            {
                msg = "产品不能包括自己！";
                return false;
            }

            if(_bomEntryModel.MaterialItemNumber!=this._oldMaterialItemNumber)
            {
                //说明修改了物料代码
                var bomEntryModelTmp =  _bomImpl.GetBomEntryModel(_bomEntryModel.ProductItemId, _bomEntryModel.ProductVersion,
                                          _bomEntryModel.MaterialItemId);
                if(bomEntryModelTmp == null)
                {
                    return true;
                }
                else
                {
                    msg = "该产品的此版本已经包含该物料！";
                    return false;
                }
            }
            else
            {
                //没有修改物料代码
                return true;
            }
        }

        private bool Save()
        {
            try
            {
                string msg;
                if (ValidateBomEntryModel( out msg))
                {
                    _bomImpl.UpdateBomEntryModel(_oldProductItemId, _oldProductVersion, _oldMaterialItemId, _bomEntryModel);
                    return true;
                }
                else
                {
                    Message.MsgBox.ShowWarning(msg);
                    return false;
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.ShowException(ex);
                return false;
            }
        }
    }
}